/* Copyright (C) 2010  Romolo Camplani, Marco Fortunato, Marco Marelli, Guido Rota, Fabio A. Schreiber et al.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.dei.perla.sys.persistence;

import org.dei.perla.parser.expressions.ConstantBlob;
import org.dei.perla.parser.expressions.ConstantFloat;
import org.dei.perla.parser.expressions.ConstantInteger;
import org.dei.perla.parser.expressions.ConstantLogic;
import org.dei.perla.parser.expressions.ConstantNull;
import org.dei.perla.parser.expressions.ConstantString;
import org.dei.perla.parser.expressions.ConstantTimestamp;
import org.dei.perla.parser.expressions.ConstantVectorFloat;
import org.dei.perla.parser.expressions.ConstantVectorInteger;
import org.dei.perla.parser.expressions.LogicValue;
import org.dei.perla.sys.query.llqruntime.datastructures.Buffer;
import org.dei.perla.sys.query.llqruntime.datastructures.QueryRecordStructure;
import org.dei.perla.sys.query.llqruntime.datastructures.Record;
/**
 * 
 * @author Luigi Rinaldi & Matteo Rovero
 *
 */
public class LoadBuffer extends Thread{
	
	
	private boolean loop = true;
	Buffer buff= new Buffer();
	private QueryRecordStructure qrs;
	private int vecInt[] = new int[10];
	private double vecFloat[] = new double[10];
	private byte blob[] = {'C','i','a','o',' ','s','o','n','o',' ','u','n',' ','b','l','o','b'};
	
	
	public LoadBuffer(){
		qrs = new QueryRecordStructure();
		qrs.addFieldStructure("Temperatura",ConstantInteger.class);
		qrs.addFieldStructure("Pressione",ConstantFloat.class);
		qrs.addFieldStructure("ID",ConstantString.class);
		qrs.addFieldStructure("Logico",ConstantLogic.class);
		qrs.addFieldStructure("Nulla",ConstantNull.class);
		qrs.addFieldStructure("TS",ConstantTimestamp.class);
		qrs.addFieldStructure("VettoreInteri",ConstantVectorInteger.class);
		qrs.addFieldStructure("VettoreFloat",ConstantVectorFloat.class);
		qrs.addFieldStructure("BL",ConstantBlob.class);
		
	}

	public void run(){
		while(loop){	
			Record rec = createRandomRecord();
			if(buff.addRecord(rec))
				System.out.println("Aggiungo il seguente record al buffer: \n" +rec.toString()+ "\n");
			else 
				System.out.println("Record non aggiunto");
			try {
				sleep((long) (Math.random()*1000));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	private Record createRandomRecord(){
		Record rec = new Record(qrs);
		rec.setField("Temperatura", new ConstantInteger((int)(Math.random()*40)));
		rec.setField("Pressione", new ConstantFloat(Math.random()));
		rec.setField("ID", new ConstantString("x154zs"+(int)(Math.random()*100)));
		switch((int)(Math.random()*10)){
			case 0: rec.setField("Logico", new ConstantLogic(LogicValue.FALSE));
				break;
			case 1: rec.setField("Logico", new ConstantLogic(LogicValue.TRUE));
				break;
			default: rec.setField("Logico", new ConstantLogic(LogicValue.UNKNOWN));
				break;
		}
		rec.setField("Nulla", new ConstantNull());
		rec.setField("TS", new ConstantTimestamp());
		
		for(int i=0; i<10; i++){
			vecInt[i] = (int)(Math.random()*10);
			vecFloat[i] = (double)(Math.random()*10); 
		}
		rec.setField("VettoreInteri", new ConstantVectorInteger(vecInt));
		rec.setField("VettoreFloat", new ConstantVectorFloat(vecFloat));
		rec.setField("BL", new ConstantBlob(blob));
		return rec;
	}

}
